<?php
error_reporting(E_ALL ^E_NOTICE);
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPayProsessStart', 'jeProcess2coStart' );
$_MAMBOTS->registerFunction( 'onPayProsessFinish', 'jeProcess2coFinish' );

function jeProcess2coFinish()
{
    global $database;
    
    $processor = mosGetParam( $_REQUEST, 'processor'); // success or fail
    if($processor != '.2co') return false;

    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$params[processor]}'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
    
    $vendor 	= $botParams->def( 'vendor');
    $secret 	= $botParams->def( 'sword');

    $key = mosGetParam( $_REQUEST, 'key' );
    $order_amount = mosGetParam( $_REQUEST, 'total');
    $order_number = mosGetParam( $_REQUEST, 'order_number');
    
    $string_to_hash = $secret . $vendor . $order_number . $order_amount;
    $check_key = strtoupper( md5( $string_to_hash ) );

    
    $credit_card_processed = mosGetParam( $_REQUEST, 'credit_card_processed');
    
    $order['id'] = mosGetParam( $_REQUEST, 'cart_order_id');
    $order['pay'] = mosGetParam( $_REQUEST, 'pay');
    $order['gateway'] = $processor;
    $order['gateway_id'] = mosGetParam( $_REQUEST, 'order_number');
    $order['price'] = mosGetParam( $_REQUEST, 'total', 0);
    
    if($check_key != $key)
    {
        //$order['pay'] = 'fail';
    }

    return $order;
}

function jeProcess2coStart( $params )
{
    Global $my;
    
    if($params['processor'] != '.2co') return;
    
    $user   = $my->id;
    $email  = $my->email;
    $option = mosGetParam( $_REQUEST, 'option');
    $task   = mosGetParam( $_REQUEST, 'task');
    $task   = 'pay';
    $product_id = ($params['item_number'] ? $params['item_number'] : $params['order_id']);
    $order_id   = $params['order_id'];
    $price      = $params['order_amount'];
    
    //echo "Start".$params['processor'];<IMG src="../../../www.jdev.1.0.x.com/templates/cnlogistic/template_thumbnail.png" width="230" height="148" alt="template_thumbnail (9K)">
    
    //return;SSSSSSSS
    
    global $database;

    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$params[processor]}'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
  
    $vendor 	= $botParams->def( 'vendor');
    $secret 	= $botParams->def( 'sword');
    
    if(!$vendor || !$price || !$user || !$order_id)
    {
        echo "<h1>Error Payment Processing: some of the params passed are empty<br>$vendor  - $price - $user -  $order_id</h1>";
        return;
    }
    
    $param['demo'] = ($botParams->def( 'demo') ? $botParams->def( 'demo') : 'Y');
    
    $param['sid'] = $vendor;
    $param['total'] = $price;
    $param['cart_order_id'] = $order_id;
    $param['fixed'] = 'Y';
    $param['c_prod'] = $product_id;
    $param['c_name'] = $params['order_title'];
    $param['c_price'] = $params['order_amount'];
    $param['c_description'] = $params['order_title'];
    $param['id_type'] = 1;
    $param['lang'] = $botParams->def( 'lang') ? $botParams->def( 'lang') : 'en';
    $param['option'] = $option;
    $param['task'] = $task;
    $param['email'] = $email;
    $param['processor'] = $params['processor'];
    $param['order_title'] = $params['order_title'];
    $param['quantity'] = $params['order_amount'];
    //$param['field'] = $field;
     
    $url = 'https://www.2checkout.com/2co/buyer/purchase?'.jpArray2Url($param);
    //echo $url;
    header("location: $url");
}

if(!defined('JP_IMPLODE'))
{
    define('JP_IMPLODE', 1);
    function jpArray2Url($param)
    {
        foreach($param AS $k => $v)
        {
            $out[] = "$k=$v";
        }
        return implode('&', $out );
    }
}



?>